package be.vdabantwerpen.gow.services;

import java.util.List;

import be.vdabantwerpen.gow.dao.PostDAO;
import be.vdabantwerpen.gow.dao.PostDAOImpl;
import be.vdabantwerpen.gow.entities.Post;
import be.vdabantwerpen.gow.entities.GOWUser;
import be.vdabantwerpen.gow.exceptions.PostNotFoundException;

public class PostServiceImpl implements PostService {
        private static final int RECENT_POSTS = 5;
	private final PostDAO postDAO = new PostDAOImpl();

	@Override
	public void create(Post post) {
		postDAO.create(post);

	}

	@Override
	public Post read(int postId) {
            Post post = postDAO.read(postId);
            if(post == null){
                throw new PostNotFoundException();
            }
            return post;
	}

	@Override
	public void update(Post post) {
		postDAO.update(post);
	}

	@Override
	public void delete(Post post) {
		postDAO.delete(post);

	}

	@Override
	public List<Post> findAll() {
            List<Post> posts = postDAO.findAll();
            if(posts == null || posts.isEmpty()){
                throw new PostNotFoundException();
            }
            return posts;
	}
        
        @Override
        public List<Post> findByUser(GOWUser user) {
            List<Post> posts = postDAO.findAll();
            if(posts == null || posts.isEmpty()){
                throw new PostNotFoundException();
            }
            for(Post post : posts){
                if(!post.getUser().getUserName().equalsIgnoreCase(user.getUserName())){
                    posts.remove(post);
                }
            }
            return posts;
        }
        
        @Override
        public List<Post> findMostRecent() {
            List<Post> posts = postDAO.findAll();
            Post[] postArray = new Post[RECENT_POSTS];
            int counter;
            for(counter = 0; counter < RECENT_POSTS && posts.size() > 0; counter++){
                postArray[counter] = posts.remove(counter);
            }
            while(posts.size() > 0){
                Post post = posts.remove(0);
                for(counter = 0; counter < RECENT_POSTS; counter++){
                    if(post.getDate().isAfter(postArray[counter].getDate())){
                        Post oldPost = postArray[counter];
                        postArray[counter] = post;
                        post = oldPost;
                    }
                }
            }
            for(counter = 0; counter < postArray.length; counter++){
                for(int i = counter; i < postArray.length; i++){
                    if(postArray[counter] != null && postArray[i] != null && 
                            postArray[counter].getDate().isBefore(postArray[i].getDate())){
                        Post post = postArray[counter];
                        postArray[counter] = postArray[i];
                        postArray[i] = post;
                    }
                }
            }
            for(counter = 0; counter < postArray.length; counter++){
                if(postArray[counter] != null){
                    posts.add(postArray[counter]);
                }
            }
            return posts;
        }

}
